package com.ipad.net;

import android.accounts.NetworkErrorException;

import com.google.gson.JsonParseException;

import org.reactivestreams.Subscription;

import io.reactivex.FlowableSubscriber;
import io.reactivex.annotations.NonNull;

/**
 * Created by Administrator on 2018/12/3.
 */

public abstract class ApiCallback<T> implements FlowableSubscriber<T> {
    public abstract void onSuccess(T o);
    public abstract void onFailuer(String msg);
    public abstract void onFinish();


    @Override
    public void onSubscribe(@NonNull Subscription s) {
        s.request(2);
    }

    @Override
    public void onNext(T o) {
        onSuccess(o);
    }

    @Override
    public void onError(Throwable throwable) {
        String msg = "";
        if (throwable instanceof JsonParseException){
            msg = "解析异常";
        }else if (throwable instanceof NetworkErrorException){
            msg = "网络异常";
        }else {
            msg = "服务异常";
        }
        onFailuer(msg);
        onFinish();
    }

    @Override
    public void onComplete() {
        onFinish();
    }
}
